﻿#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;

//两句话中的不常见单词
//https://leetcode.cn/problems/uncommon-words-from-two-sentences/submissions/
class Solution {
public:
    vector<string> uncommonFromSentences(string s1, string s2) {
        vector<string> vs;
        string s = s1 + " " + s2;
        unordered_map<string, int> m;
        int start = 0;
        int pos = 0;
        while ((pos = s.find(' ', pos)) != string::npos)
        {
            ++m[s.substr(start, pos - start)];
            start = ++pos;
        }
        ++m[s.substr(start)];

        for (const auto& x : m) if (x.second == 1) vs.push_back(x.first);

        return vs;
    }
};

//字符串压缩
//https://leetcode.cn/problems/compress-string-lcci/submissions/
class Solution {
public:
    string compressString(string S) {
        int n = S.size();
        string ret;
        ret.reserve(n / 2);
        int num = 1;
        int pos = 1;
        while (pos < n)
        {
            if (S[pos] == S[pos - 1]) ++num;
            else
            {
                ret.push_back(S[pos - 1]);
                ret += to_string(num);
                num = 1;
            }
            ++pos;
        }
        ret.push_back(S[pos - 1]);
        ret += to_string(num);
        if (ret.size() >= n) return S;
        return ret;
    }
};
